<?php
class UserTalkAction extends UAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'tpl';
		$this->layout = 'uindex';
		$this->data['menu']	= 'talk';
	}

	public function execute()
	{
		$types = array('me','atme','comments', 'all');
		$type  	   = $this->context->get('type');
		if (!in_array($type, $types)){
			$type = 'me';
		}
		if (!$this->data['is_master']){
			$type = 'weibo';
		}
		$this->data['type']	=  $type;
		$page = $this->getPage();
		$item = array(
			'user_id'=>$this->user_id,
			'username'=>'疯疯分享',
			'content'=>'挺时尚好看的，喜欢拼接风格',
			'created'=>'1个月前',
			'from_content'=>'fff',
			'logo'=>'/images/1.jpg',
			'isfollow'=>false,
		);
		
		$shareItem = array(
			'share_id'=>1,
			'user_id'=>1,
			'username'=>'筱筱07',
			'title'=>'日单外贸女装~日系VIVI米娜杂志同款波点蕾丝长袖翻领T恤打底衫',
			'content'=>'棉质格子大身，波点蕾丝拼接  黑领是在蕾丝的里边的哦',
			'pic'=>'/images/sharesmall.jpg',
			'bigpic'=>'/images/sharebig.jpg',
			'price'=>'988.00',
			'comment_num'=>1,
			'repost_num'=>12,
		);
		
		$this->data['list'] = array();
		
		$pagesize = 5;
		$weiboService  = new WeiboService();
		$con = array();
		switch($type){
			case 'weibo':
			case 'me':
				$con['where']   = array('user_id'=>'eq:'.$this->user_id);
				$con['orderby'] =  'id DESC';
				$result	= $weiboService->getList($con, ($page-1)*$pagesize, $pagesize);
				break;
			case 'atme':
				$result = array('count'=>0,'rows'=>array());
				$result = $weiboService->getListAtMe($this->user_id);
				break;
			case 'comments':
				$result = array('count'=>0,'rows'=>array());
				$result = $weiboService->getCommentsToUser($this->user_id);
				break;
			case 'all':
				$con['where'] 	= array();
				$con['orderby'] = 'id DESC';
				$result			= $weiboService->getList($con, ($page-1)*$pagesize, $pagesize);
				break;
		}
		
		
		$totalNum 	= $result['count'];
		$weibos		= $result['rows'];
		$userids 	= array();
		$parentids	= array();
		if ($totalNum)
		{
			foreach($weibos as $k=>$v)
			{
				$weibos[$k]['created'] = Util::getBeforeTimelag(strtotime($v['created']));
				$weibos[$k]['is_parent'] = false;
				$weibos[$k]['is_follow'] = $this->isFollow($v['user_id']);
				if ($v['share_id']){
					$weibos[$k]['relay_share'] = $shareItem;
				}
				if (!isset($v['username'])){
					$userids[$v['user_id']] = $v['user_id'];
				}
				if ($v['parent_id']){
					$parentids[$v['parent_id']] = $v['parent_id'];
				}
			}
			$presult = $weiboService->getListByIds($parentids);
			foreach($presult as $v){
				$userids[$v['user_id']] = $v['user_id'];
			}
			$userService = new UserService();
			$uresult = $userService->getListByIds($userids);
			foreach($weibos as $k=>$v)
			{
				if (!isset($v['username']) && isset($uresult[$v['user_id']])){
					$v += $uresult[$v['user_id']];
				}
				if ($v['parent_id'] && isset($presult[$v['parent_id']])){
					if (isset($uresult[$presult[$v['parent_id']]['user_id']])){
						$presult[$v['parent_id']] += $uresult[$presult[$v['parent_id']]['user_id']];
					}
					$v['is_parent']    = true;
					$v['parent_weibo'] = $presult[$v['parent_id']];
				}
				$weibos[$k] = $v;
			}
		}
		$this->data['comments'] = $comments;
		$this->data['list'] = $weibos;
		if ($totalNum){
			$ltConfig		= C('LtConfig');
			$pagerConfig	= $ltConfig->get('pagination.pager');
			$baseUrl 		= C('LtUrl')->generate('User', 'Talk', array('type'=>$type,'uid'=>$this->user_id,'p' => ':page'));
			$pagination 	= new PiscesPagination;
			$pagination->init($pagerConfig);
			$this->data['pages'] = $pagination->pager($page, $totalNum, $baseUrl);
		}
	}
}
